package com.zhipu.balance.ui.vm.home.calculate.dami

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordDaMi
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 大米杂质
 */
class DaMiImpurityViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 大米实体
     */
    var mRecordDaMi : RecordDaMi? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 大米-试样质量
     */
    var riceSampleQuality = ObservableField("")
    /**
     * 大米-杂质
     */
    var riceImpurity = ObservableField("")

    /**
     * 杂质率
     */
    var impurityResults = ObservableField("")


    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordDaMi?.impurityRecorder)) {
            //大米-试样质量
            riceSampleQuality.set(mRecordDaMi?.riceSampleQuality.toString())
            //大米-杂质
            riceImpurity.set(mRecordDaMi?.riceImpurity.toString())
            //杂质率
            impurityResults.set(mRecordDaMi?.impurityResults.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //大米-试样质量
        riceSampleQuality.set("")
        //大米-杂质
        riceImpurity.set("")
        //杂质率
        impurityResults.set("")
    }






    /**
     * 大米-试样质量
     */
    fun BulkQuality(){
        riceSampleQuality.set(weighing)
        mRecordDaMi?.riceSampleQuality = riceSampleQuality.get()?.toDouble()
    }

    /**
     * 大米-杂质
     */
    fun damiImpurity(){
        riceImpurity.set(weighing)
        mRecordDaMi?.riceImpurity = riceImpurity.get()?.toDouble()
    }


    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算杂质
         */
        val RatioImpurities: Double = CommonUtils.formatDouble(mRecordDaMi!!.riceImpurity / mRecordDaMi!!.riceSampleQuality * 100)
        impurityResults.set(RatioImpurities.toString())
        mRecordDaMi!!.impurityResults = RatioImpurities


        /**
         * 杂质记录员
         */
        mRecordDaMi!!.impurityRecorder = mPersonnel!!.name


        //大样质量
        if(mRecordDaMi?.riceSampleQuality == 0.00){
            riceSampleQuality.set("0.00")
        }
        //大样杂质
        if(mRecordDaMi?.riceImpurity == 0.00){
            riceImpurity.set("0.00")
        }
    }


}